/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.tsdemo.web.module;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.tsdemo.entity.module.Module;
import com.fosung.frame.tsdemo.service.module.ModuleService;

/**
 * 模块结构表Controller
 * @author zhaofj
 * @version 2017-07-05
 */
@Controller
@RequestMapping(value = "${adminPath}/tsdemo/module/module")
public class ModuleController extends BaseController {

	@Autowired
	private ModuleService moduleService;
	
	@ModelAttribute
	public Module get(@RequestParam(required=false) String id) {
		Module entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = moduleService.get(id);
		}
		if (entity == null){
			entity = new Module();
		}
		return entity;
	}
	
	@RequiresPermissions("tsdemo:module:module:view")
	@RequestMapping(value = {"list", ""})
	public String list(Module module, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Module> page = moduleService.findPage(new Page<Module>(request, response), module); 
		model.addAttribute("page", page);
		return "frame/tsdemo/module/moduleList";
	}

	@RequiresPermissions("tsdemo:module:module:view")
	@RequestMapping(value = "form")
	public String form(Module module, Model model) {
		//行业
		String modelIds=module.getModelIds();
		if(modelIds!=null&&!modelIds.equals("")){
			List<String> result = Arrays.asList(modelIds.split(",")); 
			module.setModelIdsList(result);
		}
		//层级
		String levelIds=module.getLevelIds();
		if(levelIds!=null&&!levelIds.equals("")){
			List<String> result = Arrays.asList(levelIds.split(",")); 
			module.setLevelIdsList(result);
		}
		model.addAttribute("module", module);
		return "frame/tsdemo/module/moduleForm";
	}

	@RequiresPermissions("tsdemo:module:module:edit")
	@RequestMapping(value = "save")
	public String save(Module module, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, module)){
			return form(module, model);
		}
		//行业
		List<String> modelIdsList=module.getModelIdsList();
		if(modelIdsList.size()!=0){
			String str = StringUtils.join(modelIdsList.toArray(), ","); 
			module.setModelIds(str);
		}
		//层级
		List<String> levelIdsList=module.getLevelIdsList();
		if(levelIdsList.size()!=0){
			String str = StringUtils.join(levelIdsList.toArray(), ","); 
			module.setLevelIds(str);
		}
		module.setUpdateDate(new Date());
		moduleService.save(module);
		addMessage(redirectAttributes, "保存模块结构表成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/module/module/?repage";
	}
	
	@RequiresPermissions("tsdemo:module:module:edit")
	@RequestMapping(value = "delete")
	public String delete(Module module, RedirectAttributes redirectAttributes) {
		moduleService.delete(module);
		addMessage(redirectAttributes, "删除模块结构表成功");
		return "redirect:"+Global.getAdminPath()+"/tsdemo/module/module/?repage";
	}

}